home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / BINARY_N.H < prev    next >
C/C++ Source or Header  |  1992-08-20  |  5KB  |  133 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 06/28/89 -- Initial design
  13. // Updated: MBN 07/19/89 -- Derived from Base_Binary_Node class
  14. // Updated: DKM 11/05/89 -- Added accessors to avl_balance
  15. // Updated: MJF 06/30/90 -- Added base class name to constructor initializer
  16. // Updated: VDN 02/21/92 -- New lite version and fix memory leaks
  17. // Updated: JAM 08/19/92 -- modernized template syntax, remove macro hacks
  18. //
  19. // The Binary_Node<Type> class implements parameterized nodes for binary trees.
  20. // This class  is privately  derived from the  Base_Binary_Node class  that contains
  21. // left and right subtree  pointers.   The Binary_Node<Type> class adds  a data
  22. // slot of  the required type  in the private section.    The Binary_Node<Type>
  23. // class is  intended for the sole  use of  the  Binary_Tree<Type>  class.  All
  24. // constructors and protected and the Binary_Tree<Type> class is  declared as a
  25. // friend class.
  26. //
  27. // There are three protected constructors  for the Binary_Node<Type>class.  The
  28. // first takes no arguments  and initializes the two subtree  pointers  to NULL
  29. // via the Base_Binary_Node constructor.  The second takes an  argument of type Type
  30. // and initializes the data slot to that value.  The third takes a reference to
  31. // another Binary_Node<Type> object and duplicates its values.
  32. //
  33. // Methods are provided to set and get the node data value, determine if a node
  34. // is a leaf or the root of some subtree,  and implement member-wise assignment
  35. // from one Binary_Node<Type> to another via the overloaded operator=.
  36. //
  37.  
  38. #ifndef BINARY_NODEH                // If no definition for class
  39. #define BINARY_NODEH
  40.  
  41. #ifndef BASE_BINARY_NODEH            // If no definition for class
  42. #include <cool/Base_Binary_Node.h>        // Include useful defintions
  43. #endif    
  44.  
  45. template <class Type>
  46. class CoolBinary_Tree;            // Forward reference class
  47.  
  48. template <class Type>
  49. class CoolBinary_Node : public CoolBase_Binary_Node {
  50.   friend class CoolBinary_Tree<Type>;        // CoolBinary_Tree class is friend
  51. public:  
  52.   CoolBinary_Node();            // Simple constructor
  53.   CoolBinary_Node(const Type&);        // Constructor with data value
  54.   CoolBinary_Node(const CoolBinary_Node<Type>&);    // Copy constructor
  55.   virtual ~CoolBinary_Node();        // Destructor is virtual
  56.  
  57.   inline void set (Type&);            // Set node data to value
  58.   inline Type& get () const;            // Get node data value
  59.   inline void set_ltree (CoolBinary_Node<Type>*);  // Accessor to set ltree pointer
  60.   inline CoolBinary_Node<Type>* get_ltree () const;// Accessor to get ltree pointer
  61.   inline void set_rtree (CoolBinary_Node<Type>*);  // Accessor to set rtree pointer
  62.   inline CoolBinary_Node<Type>* get_rtree () const;// Accessor to get rtree pointer
  63.  
  64.   CoolBinary_Node<Type>& operator= (const CoolBinary_Node<Type>&); // Overload assignment
  65.  
  66. protected:
  67.   Type data;                    // Slot to hold data value
  68.   CoolBinary_Node<Type>* copy_nodes (const CoolBinary_Node<Type>*) const; // Copy of subnodes
  69. };
  70.  
  71.  
  72. // set -- Set value of data slot in node
  73. // Input: Reference to data slot value
  74. // Output: None
  75.  
  76. template <class Type>
  77. inline void CoolBinary_Node<Type>::set (Type& value) {
  78.   this->data = value;                // Set data slot value
  79. }
  80.  
  81.  
  82. // get -- Get value of data slot in node
  83. // Input: None
  84. // Output: Reference to data slot value
  85.  
  86. template <class Type>
  87. inline Type& CoolBinary_Node<Type>::get () const {
  88.   return ((CoolBinary_Node<Type>*) this)->data; // avoid warning & const
  89. }                        // Return data slot value
  90.  
  91.  
  92. // set_ltree -- Accessor to set left subtree pointer in base class
  93. // Input:       CoolBinary_Node pointer
  94. // Output:      None
  95.  
  96. template <class Type>
  97. inline void CoolBinary_Node<Type>::set_ltree (CoolBinary_Node<Type>* bn) {
  98.   this->ltree = bn;
  99. }
  100.  
  101.  
  102. // set_rtree -- Accessor to set right subtree pointer in base class
  103. // Input:       CoolBinary_Node pointer
  104. // Output:      None
  105.  
  106. template <class Type>
  107. inline void CoolBinary_Node<Type>::set_rtree (CoolBinary_Node<Type>* bn) {
  108.   this->rtree = bn;
  109. }
  110.  
  111.  
  112. // get_ltree -- Accessor to get left subtree pointer in base class
  113. // Input:       None
  114. // Output:      CoolBinary_Node pointer
  115.  
  116. template <class Type>
  117. inline CoolBinary_Node<Type>* CoolBinary_Node<Type>::get_ltree () const {
  118.   return (CoolBinary_Node<Type>*)this->ltree;
  119. }
  120.  
  121.  
  122. // get_rtree -- Accessor to get right subtree pointer in base class
  123. // Input:       None
  124. // Output:      CoolBinary_Node pointer
  125.  
  126. template <class Type>
  127. inline CoolBinary_Node<Type>* CoolBinary_Node<Type>::get_rtree () const {
  128.   return (CoolBinary_Node<Type>*)this->rtree;
  129. }
  130.  
  131.  
  132. #endif                        // End BINARY_NODEH #if
  133.